//express_demo.js 文件
var express = require("express");
const parse = require("body-parser");
var app = express();

// express自带的解析格式
app.use(express.urlencoded({ extended: false })); //解析x-www-form格式
app.use(express.json()); //解析json格式

// 使用第三方中间件
// app.use(parse.urlencoded({ extended: false }));
// app.use(parse.json());

app.post("/reg", function (req, res) {
  // 判断Content-Type请求头
  console.log(req.headers);

  const contentType = req.headers["content-type"];
  if (contentType.includes("application/json")) {
    console.log("Received JSON data:", req.body);
    res.setHeader("Content-Type", "application/json");
  }
  if (contentType.includes("application/x-www-form-urlencoded")) {
    console.log("Received x-www-form-urlencoded data:", req.body);
    res.setHeader("Content-Type", "application/x-www-form-urlencoded");
  }
  res.send({ message: "注册成功" });
});
app.listen(8080);
